import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {MessageService} from "../services/message.service";
import {LoginService} from "../services/login.service";
import {Router} from '@angular/router';


@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.css']
})
export class LoginComponent implements OnInit {
  // 密码
  pwd = '';
  //
  doRemove = {
    type: true,
    str: '手机号不能为空',
    curr: false
  };
  doP = {
    type: true,
    str: '请输入密码',
    curr: false
  };
  //
  telStr = {
    login: '你好,',
    exit: '退出'
  };
  //
  str = '';
  //
  bClick = false;
  //
  bHome: Array<any>;
  constructor(private http: HttpClient, private msg: MessageService, private lg: LoginService, private router: Router) { }

  ngOnInit() {
  }
  doClick() {
    if (this.doRemove.curr) {
      if (this.doP.curr) {
        this.http.post('/account/post', {'tel': this.str, 'pwd': this.pwd}).subscribe((data) => {
          if (data['name'] === '登录成功') {
            // this.bHome = ['/'];
            this.router.navigate(['/home']);
            this.msg.str = '你好,' + this.str;
            // 发送你好,手机号
            this.telStr.login = this.telStr.login + this.str;
            this.lg.str = this.telStr.login + this.str;
            this.lg.sendMessage(this.telStr);

          } else {
            alert(data['name']);
          }
        });

      } else {
        this.dobp();
      }
    } else {
      this.doBlur();
    }
  }
  // 获得焦点
  doFocus() {
    this.doRemove.type = false;
  }
  doPwd() {
    this.doP.type = false;
  }
  enter() {
    this.bClick = !this.bClick;
    console.log(this.bClick);
  }
  // 失去焦点
  doBlur() {
    console.log(this.str)
    if (this.str !== '') {
      if ((/^1[3|4|5|8][0-9]\d{4,8}$/.test(this.str))) {
        this.doRemove.type = false;
        this.doRemove.curr = true;
      } else {
        this.doRemove.type = true;
        this.doRemove.str = '手机号码不正确，请重新输入';
      }
    } else {
      this.doRemove.type = true;
    }

  }

  dobp() {
    if (this.pwd !== '') {
      if ((/^\w{6,16}$/.test(this.pwd))) {
        this.doP.type = false;
        this.doP.curr = true;
      } else {
        this.doP.type = true;
        this.doP.str = '密码简单，请重新输入';
      }
    } else {
      this.doP.type = true;
    }
  }
}
